﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using SmartMD.DataAccess.Oracle;
using SmartMD.DataModel.Entity;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Techstar.Dialog;
using SmartMD.Manager.Utils;
using System.Collections;

namespace SmartMD.Manager.Specimens
{
    /// <summary>
    /// Interaction logic for QuerySpecEditPad.xaml
    /// </summary>
    public partial class QuerySpecEditPad : BasePad
    {
        public QuerySpecEditPad()
        {
            InitializeComponent();
        }
        public QuerySpecPanel Owner;       

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            //清空列表
            if (this.Owner.ucSpecQueryList.SpecList != null)
            {
                this.Owner.ucSpecQueryList.SpecList.Clear();
            }

            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append("select distinct t3.ENTITYID as ID, t.NAME as SPECNAME, t2.NAME as PATIENTNAME, t3.SITECODE, t3.ENTITYSTATE, t.SUBJECT, t.COLLECTER, t3.STOREDDATE, t3.FREEZETHRAWTIMES,  t.SPECTYPE"); //t4.FOLLOWUPTIMES,
            sb.Append(" from DUO_SPECIMENS t, DUO_PATIENT t2, DUO_SPEC_ENTITY t3 "); //, duo_followup_info t4

            sb.Append(" where t3.SPECID = t.SPECID");
            sb.Append(" and t.PATIENTID = t2.PATIENTID");

            if (this.cb_id.IsChecked == true)    //标本标识
            {
                String idTxt = this.txtSpecId.Text;
                sb.Append(" and t.SPECID = '" + idTxt + "'");
            }
            if (this.cb_site.IsChecked == true)  //位置编码
            {
                String specSiteTxt = this.txtSpecSite.Text;
                sb.Append(" and t3.SITECODE = '" + specSiteTxt + "'");
            }
            if (this.cb_subject.IsChecked == true) //所属课题
            {
                String subjectTxt = this.txtSubject.Text;
                sb.Append(" and t.SUBJECT = '" + subjectTxt + "'");
            }
            if (this.cb_patient.IsChecked == true)  //病人标识
            {
                String patientTxt = this.txtPatient.Text;
                sb.Append(" and t2.name = '" + patientTxt + "'");
            }
            if (this.cbDiagnose.IsChecked == true)  //疾病诊断
            {
                String nameTxt = this.txtDiagnose.Text;

                //sb.Append(" and t2.PATIENTID = t4.PATIENTID");
                //sb.Append(" and t4.diagnose = '" + nameTxt + "'");
            }
           /* if (this.cb_container.IsChecked == true)  //所属容器
            {
                String conNameTxt = this.txtContainer.Text;        
                sb.Append(" and t3.containerid = t.containerid");
                sb.Append(" and t3.name = '" + conNameTxt + "'");
            }*/
            if (this.cb_timerange.IsChecked == true)  //时间范围
            {
                DateTime startTime = DateTime.Parse(this.specStartTime.Text);
                DateTime endTime = DateTime.Parse(this.specEndTime.Text);
                sb.Append(" t.FOLLOWUPDATE >= " + "to_date ( '" + startTime.ToString("yyyy-MM-dd HH:mm:ss") + "' , 'YYYY-MM-DD HH24:MI:SS' )");
                sb.Append(" and t.FOLLOWUPDATE <= " + "to_date ( '" + endTime.ToString("yyyy-MM-dd HH:mm:ss") + "' , 'YYYY-MM-DD HH24:MI:SS' )");
            }   
          
            DataTable dt = ConnectDatabase.getInstance().Select(sb.ToString());
            ObservableCollection<SpecQueryResult> resultList = new ObservableCollection<SpecQueryResult>();
            SpecQueryResult item = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    item = new SpecQueryResult();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        item.setValue(clmName, dt.Rows[j][clmName]);
                        item.State = DataModel.RecordState.Normal;
                     
                    }
                    resultList.Add(item);
                }
                this.Owner.ucSpecQueryList.SpecList = resultList;
            }
            else
            {
                TaskDialogEx.Warning("查询失败", "未查到您所查询的标本！");
            }

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.txtSpecId.Text = "";
            this.txtDiagnose.Text = "";
            this.txtPatient.Text = "";
            this.txtContainer.Text = "";
            this.txtSpecSite.Text = "";
        }

        private Hashtable _cmbHashtable = new Hashtable();
        private void cmbDiagnose_Initialized(object sender, EventArgs e)
        {
            _cmbHashtable.Add("疾病诊断", sender);
            ComboxUtil.InitializeComboxByDict(sender, "疾病诊断");
        }
        public void InitAllComboxValue()
        {
            string key;
            object sender;
            foreach (DictionaryEntry de in _cmbHashtable)
            {
                key = de.Key.ToString();
                sender = de.Value as object;
                ComboxUtil.InitializeComboxByDict(sender, key);
            }
        }
    }
}
